package com.si.anddos.search;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class SearchResultHandler extends DefaultHandler {
	
	// names of the XML tags
	private static final String ADDED_DATE = "added";
    private static final String DESCRIPTION = "description";
    private static final String NAME = "name";
    private static final String EXTENSION = "extension";
    private static final String SIZE = "size";
    private static final String ADDRESS = "address";
    private static final String DETAILS = "details";
    
    private static final String ERROR = "error";
    private static final String HASRESULTS = "hasResults";
    private static final String HITSTOTAL = "hitsTotal";
    
    
	
	private StringBuffer buffer = new StringBuffer();
	
	private ArrayList<SearchResult> searchResultList;
	private SearchResult searchResult;
	
	private boolean wasError = false;
	private boolean hasResults;
	
	private int hitsTotal;
	
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
		buffer.setLength(0);
		
		if (localName.equals("results")) {
			searchResultList = new ArrayList<SearchResult>();
		}
		else if (localName.equals("hits")) {
			searchResult = new SearchResult();
		}
		else if (localName.equals(ERROR)) {
			wasError = true;
		}
		

	}
	
	@Override
	public void endElement(String uri, String localName, String qName)throws SAXException {
		
		if (localName.equals(HASRESULTS)) {
			if(Integer.valueOf(buffer.toString()).intValue() == 0)
				hasResults = false;
			else
				hasResults = true;
		}
		else if (localName.equals(HITSTOTAL)) {
			hitsTotal = Integer.valueOf(buffer.toString()).intValue();
		}
		else if (localName.equals("hits")) {
			searchResultList.add(searchResult);
		}
		else if (localName.equals(NAME)) {
			searchResult.setName(buffer.toString());
		}
		else if (localName.equals(EXTENSION)) {
			searchResult.setExtension(buffer.toString());
		}
		else if (localName.equals(SIZE)) {
			searchResult.setSize(buffer.toString());
		}
		else if (localName.equals(DESCRIPTION)) {
			searchResult.setDescription(buffer.toString());
		}
		else if (localName.equals(ADDRESS)) {
			searchResult.setAddress(buffer.toString());
		}
		else if (localName.equals(DETAILS)) {
			searchResult.setDescription(buffer.toString());
		}
		else if (localName.equals(ADDED_DATE)) {
			searchResult.setAdded(buffer.toString());
		}
		
		
	}
	
	@Override
	public void characters(char[] ch, int start, int length) {
		buffer.append(ch, start, length);
	}
	
	public int wasSucessfull(){
		if(wasError) return -1;
		if(hasResults) return hitsTotal;
		return 0;
	}
	public ArrayList<SearchResult> retrieveSearchResultList() {
		if(searchResultList == null) searchResultList = new ArrayList<SearchResult>();
		return searchResultList;
	}
	
}
